package Text2;

public class MyLinkedList implements LinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后继

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//头节点
    public ListNode last;//尾节点


    @Override
    public void addFirst(int data) {
        ListNode listNode = new ListNode(data);
        if (head == null) {
            head = listNode;
            last = listNode;
            return;
        }
        head.prev = listNode;
        listNode.next = head;
        head = listNode;
    }

    @Override
    public void addLast(int data) {
        ListNode listNode = new ListNode(data);
        if (head == null) {
            head = listNode;
            last = listNode;
            return;
        }
        last.next = listNode;
        listNode.prev = last;
        last = listNode;
    }

    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = index;
        while (count <= 0) {
            cur = cur.next;
            count--;
        }
        return cur;
    }

    @Override
    public boolean addIndex(int index, int data) throws ListIndexOutOfException {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("违规数据");
        }
        if (index == 0) {
            addFirst(data);
            return true;
        }
        if (index == size()) {
            addLast(data);
            return true;
        }
        ListNode cur = findIndex(index);
        ListNode listNode = new ListNode(data);
        listNode.next = cur;
        listNode.prev = cur.prev;
        cur.prev.next = cur;
        cur.prev = cur;
        return true;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                if(cur==head){
                    head=head.next;
                    if(head==null){
                        last=null;
                    }else {
                        head.prev=null;
                    }
                }else{
                    cur.prev.next=cur.next;
                    if(cur==last){
                        last=last.prev;
                    }else {
                        cur.next.prev=cur.prev;
                    }
                }
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.val==key){
                //开始删除了
                if(cur==head){
                    //1. 删除的是头节点
                    head =head.next;
                    //只有一个节点
                    if(head==null){
                        last=null;
                    }else{
                        head.prev=null;
                    }
                }else{
                    //中间  尾巴
                    cur.prev.next=cur.next;
                    //是尾巴节点
                    if(cur==last){
                        last=last.prev;
                    }else{
                        //不是尾巴节点
                        cur.next.prev=cur.prev;
                    }
                }
            }
            cur=cur.next;
        }
    }

    @Override
    public int size() {
        int len = 0;
        ListNode cur = head;
        while (cur != null) {
            len++;
            cur = cur.next;
        }
        return len;

    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();

    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            cur.prev = null;
            cur = cur.next;
            cur.prev.next=null;
        }
        head=null;
        last=null;
    }
}
